package com.laboratory.serviceprovide.setting.service.impl;

import com.Laboratory.setting.service._noticeService;
import com.Laboratory.setting.service._roomService;
import com.Laboratory.workBench.pojo.room;
import com.alibaba.fastjson.JSON;
import com.laboratory.serviceprovide.constant.constantArgs;
import com.laboratory.serviceprovide.workBench.mapper.roomMapper;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @Author acok
 * @description:TODO
 * @Date 2023/1/2 9:04
 * @Version 1.0
 */
@DubboService(interfaceClass = _roomService.class,version = "1.0.0")
public class _roomServiceImpl implements _roomService {

    @Resource
    private roomMapper roomMapper;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    private void updateCache(int result){
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        if(result>0){
            //更新房间相关缓存
            if (ops.get(constantArgs.REDIS_ROOM_NO_KEY)!=null){
                List<Map<Object,Object>>roomNoList=roomMapper.getRoomMsgList();
                ops.setIfPresent(constantArgs.REDIS_ROOM_NO_KEY, JSON.toJSONString(roomNoList),
                        30 * 60, TimeUnit.SECONDS);
            }
        }
    }

    @Override
    public List<room> getRoomListByCondition(String roomLocation, Integer number) {
        return roomMapper.getRoomListByCondition(roomLocation, number);
    }

    @Override
    public List<String> getRoomLocationList() {
        return roomMapper.getRoomLocationList();
    }

    @Override
    public int insertRoom(room room) {
        int i = roomMapper.insertRoom(room);
        updateCache(i);
        return i;
    }

    @Override
    public int updateRoomById(room room) {
        int j = roomMapper.updateRoomById(room);
        updateCache(j);
        return j;
    }

    @Override
    public int deleteRoomByIds(String[] ids) {
        //根据主键数组删除教室记录，存在今天、明天预约记录以及被占用房价无法被预约
        int i = roomMapper.deleteRoomByIds(ids);
        //更新要删除房间未删除成功时（以上两种情况），将教室状态改为锁定，已删除记录，则已无法匹配
        roomMapper.updateRoomStateByIds(ids);
        //根据主键数组删除预约记录每当教室在今天、明天存在预约记录时不予以删除
        roomMapper.deleteBookingByRoomIdsForDelRoom(ids);
        updateCache(i);
        return i;
    }

    @Override
    public int deleteInvalidBookingRecord(String timeSign) {
        return roomMapper.deleteInvalidBookingRecord(timeSign);
    }
}
